﻿package com.chandings.view.components
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Stage;
	import flash.events.EventDispatcher;
	
	/**
	 * ...
	 */
	public class SpaceShip extends EventDispatcher
	{
		protected var _canvas:DisplayObjectContainer;
		protected var _spaceShipDO:DisplayObject;
		protected var _spaceShipDODup:DisplayObject;
		
		protected var _velocity:Number=0;
		
		protected const _minVelocity:Number=0;
		protected const _maxVelocity:Number = 20;
		
		protected var _minRotation:Number=-5;
		protected var _maxRotation:Number=5;
		
		protected var _accelaration:Number=.5;
		protected var _friction:Number = .99;
		
		protected var _rotationalAccelaration:Number=.3;
		protected var _rotationalFriction:Number = .9;
		
		protected var _rotation:Number;
		
		public function SpaceShip() 
		{
			init();
		}
		
		public function setStage(root:DisplayObjectContainer):void
		{
			_canvas = root;
			_spaceShipDO = new SpaceShipSprite() as DisplayObject;
			_spaceShipDO.x = _canvas.stage.stageWidth / 2;
			_spaceShipDO.y = _canvas.stage.stageHeight / 2;
			_spaceShipDO.rotation -= 90;
			_canvas.addChild(_spaceShipDO);
		}
		
		private function init():void
		{
			
		}
		
		public function get velocity():Number { return _velocity; }
		
		public function set velocity(value:Number):void 
		{
			if (value > _maxVelocity)
			{
				value = _maxVelocity;
			}
			else if (value < _minVelocity)
			{
				value = _minVelocity
			}
			
			_spaceShipDO.y += value*Math.sin(_spaceShipDO.rotation*Math.PI/180);
			_spaceShipDO.x += value*Math.cos(_spaceShipDO.rotation*Math.PI/180);
			_velocity = value;
		}
		
		public function get accelaration():Number { return _accelaration; }
		
		public function get friction():Number { return _friction; }
		
		public function get rotation():Number { return _rotation; }
		
		public function set rotation(value:Number):void 
		{
			_maxRotation = _velocity / 4;
			_minRotation = -1*_maxRotation;
			
			if (value > _maxRotation)
			{
				value = _maxRotation;
			}
			else if (value < _minRotation)
			{
				value = _minRotation
			}
			value = value % 360;
			_spaceShipDO.rotation += value;
			_rotation = value;
		}
		
		public function get rotationalAccelaration():Number { return _rotationalAccelaration; }
		
		public function get rotationalFriction():Number { return _rotationalFriction; }
		
	}
	
}